﻿namespace Huayou.Common
{
    using System;

    public class BinaryTreeNode
    {
        protected BinaryTreeNode left;
        protected BinaryTreeNode parent;
        protected BinaryTreeNode right;
        protected object val;

        public BinaryTreeNode(object val)
        {
            this.val = val;
            this.parent = this.left = (BinaryTreeNode) (this.right = null);
        }

        public BinaryTreeNode(object val, BinaryTreeNode left, BinaryTreeNode right) : this(val)
        {
            this.SetLeft(left);
            this.SetRight(right);
        }

        public BinaryTreeNode GetLeft()
        {
            return this.left;
        }

        public BinaryTreeNode GetParent()
        {
            return this.parent;
        }

        public BinaryTreeNode GetRight()
        {
            return this.right;
        }

        public object GetValue()
        {
            return this.val;
        }

        public bool IsLeftChild()
        {
            return ((this.parent != null) && (this.parent.GetLeft() == this));
        }

        public bool IsRightChild()
        {
            return ((this.parent != null) && (this.parent.GetRight() == this));
        }

        public void SetLeft(BinaryTreeNode newLeft)
        {
            if ((this.left != null) && (this.left.GetParent() == this))
            {
                this.left.SetParent(null);
            }
            this.left = newLeft;
            if (this.left != null)
            {
                this.left.SetParent(this);
            }
        }

        protected void SetParent(BinaryTreeNode newParent)
        {
            this.parent = newParent;
        }

        public void SetRight(BinaryTreeNode newRight)
        {
            if ((this.right != null) && (this.right.GetParent() == this))
            {
                this.right.SetParent(null);
            }
            this.right = newRight;
            if (this.right != null)
            {
                this.right.SetParent(this);
            }
        }

        public void SetValue(object val)
        {
            this.val = val;
        }
    }
}

